package com.jin.webvideo.util.base;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
    public static final String FORMAT_DEFAULT = "yyyy/MM/dd-HH:mm:ss";
    public static final String DATE_DEFAULT = "1949/10/1-00:00:00";
    public static String formatHourBefore(int hour){
        Date date = addDate(new Date(), 0, 0, 0, -hour, 0, 0);
        return format(date, FORMAT_DEFAULT);
    }
    public static final Date addDate(Date date, int year, int month, int day, int hour, int min, int second){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.YEAR, year);
        calendar.add(Calendar.MONTH, month);
        calendar.add(Calendar.DATE, day);
        calendar.add(Calendar.HOUR, hour);
        calendar.add(Calendar.MINUTE, min);
        calendar.add(Calendar.SECOND, second);
        return calendar.getTime();
    }

    public static final Date addSecond(Date date, long second) {
        return new Date(date.getTime() + second*1000);
    }
    public static final String format(Date date , String formatStr){
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        return format.format(date);
    }

    public static final Date format(String date , String formatStr){
        SimpleDateFormat format = new SimpleDateFormat(formatStr);
        try {
            return format.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
